using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class FX_FXTypeService : BaseAppService<FX_FXTypeDto, FX_FXType, FX_FXTypeRepository>
    {
        readonly ExcelHelp _excelHelp;
        public FX_FXTypeService(FX_FXTypeRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<FX_FXType, FX_FXTypeDto>
            GetLoadPageOffsetMode(PagingModel<FX_FXTypeDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("ProductionActivityName", "=") //风险类别名称
     .Add("PId", "=") //上级风险类别ID
     .Add("FieldCode", "=") //风险大类代码
     .Add("AreaID", "=") //区域ID
     .Add("FSort", "=") //排列顺序
     .Add("remarks", "=") //备注
     .Add("IsDel", "=") //删除标志
     .Add("CreateFactory", "=") //隶属工厂
     .Add("CreateDept", "=") //
     .Add("UpdateDept", "=");//


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new FX_FXType()
            {
                Id = model.Id,
                ProductionActivityName = model.ProductionActivityName,//风险类别名称
                PId = model.PId,//上级风险类别ID
                FieldCode = model.FieldCode,//风险大类代码
                AreaID = model.AreaID,//区域ID
                FSort = model.FSort,//排列顺序
                remarks = model.remarks,//备注
                IsDel = model.IsDel,//删除标志
                CreateFactory = model.CreateFactory,//隶属工厂
                CreateDept = model.CreateDept,//
                UpdateDept = model.UpdateDept,//
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<FX_FXTypeDto>> GetList(PagingModel<FX_FXTypeDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<FX_FXTypeDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.ProductionActivityName,
                ProductionActivityName_title = "风险类别名称",
                model.PId,
                PId_title = "上级风险类别ID",
                model.FieldCode,
                FieldCode_title = "风险大类代码",
                model.AreaID,
                AreaID_title = "区域ID",
                model.FSort,
                FSort_title = "排列顺序",
                model.remarks,
                remarks_title = "备注",
                model.IsDel,
                IsDel_title = "删除标志",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
                model.CreateDept,
                CreateDept_title = "",
                model.UpdateDept,
                UpdateDept_title = "",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
